<?php
namespace iBrand\Component\Order\Providers;

use iBrand\Component\Order\Models\Order;
use iBrand\Component\Order\Observers\OrderObserver;
use iBrand\Component\Order\OrderService;
use iBrand\Component\Order\Policies\OrderPolicy;
use iBrand\Component\Order\Repositories\Eloquent\OrderRepositoryEloquent;
use iBrand\Component\Order\Repositories\OrderRepository;
use iBrand\Component\Order\Schedule;
//use iBrand\Notifications\Schedule\OrderRemindSchedule;
use Event;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;

/**
 * Service provider for Laravel.
 */
class OrderServiceProvider extends ServiceProvider
{
    protected $policies = [
        Order::class => OrderPolicy::class,
    ];

    /**
     * 要注册的订阅者类。
     *
     * @var array
     */
    protected $subscribe = [
        'iBrand\Component\Order\Listeners\OrderEventListener',
    ];


    /**
     * Boot the provider.
     */
    public function boot(GateContract $gate)
    {
        if ($this->app->runningInConsole()) {
            $this->registerMigrations();
        }
        //require __DIR__ . '/../helpers.php';

        $this->registerPolicies($gate);

        foreach ($this->subscribe as $subscriber) {
            Event::subscribe($subscriber);
        }

        Order::observe(OrderObserver::class);
    }

    private function registerPolicies(GateContract $gate)
    {
        foreach ($this->policies as $key => $value) {
            $gate->policy($key, $value);
        }
    }

    /**
     * Register the service provider.
     */
    public function register()
    {

        $this->app->bind(OrderRepository::class, OrderRepositoryEloquent::class);
    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    /*public function provides()
    {
        return [OrderService::class, 'order'];
    }*/

    /**
     * Register Passport's migration files.
     *
     * @return void
     */
    protected function registerMigrations()
    {
        return $this->loadMigrationsFrom(__DIR__ . '/../../migrations');
    }
}
